


var Reg = {
		
		init:function() {
			
			//添加自定义验证规则
			Reg.ctrl.addCustomeValidate();
			//表单验证
			Reg.ctrl.initValidateForm();
		},
		ctrl:{
			/***
			 * 添加自定义验证规则
			 */
			addCustomeValidate:function() {
				  // 手机号码验证      
			    jQuery.validator.addMethod("isMobile", function(value, element) {      
			      var length = value.length;      
			      return this.optional(element) || (length == 11 && /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/.test(value));      
			    }, "请正确填写您的手机号码。"); 
				
				jQuery.validator.addMethod("gt", function(value, element, param) {
					return value > param;
					}, $.validator.format("输入值必须大于{0}!"));
				
				jQuery.validator.addMethod("isnum", function(value, element) {
					return this.optional(element) || /^[0-9]+$/.test(value);
					}, "只能是数字");
				
				  // 判断中文字符   
			    jQuery.validator.addMethod("isChinese", function(value, element) {         
			         return this.optional(element) || /^[\u0391-\uFFE5]+$/.test(value);         
			    }, "只能包含中文字符。");  
			    
			},
			
			/***
			 * 初始化表单验证
			 */
			initValidateForm:function(){
				
				$('#regForm').validate({
					rules:{
						userName:{
							required:true,
							remote:{
								url:basePath +"reg/checkUserName.mvc",  
				                 type:"post",  
				                 dataType:"json",  
				                 dataFilter: function(data, type) {  
				                	 data = eval('(' + data +')');
				                      if (data.success == true)  
				                          return false;  
				                      else  
				                          return true;  
				                 }  
							},
							rangelength:[3,12]
						},
						nickName:{
							required:true,
							rangelength:[2,10],
							isChinese:true,
							remote:{
								url:basePath +"reg/checkNickName.mvc",  
				                 type:"post",  
				                 dataType:"json",  
				                 dataFilter: function(data, type) {  
				                	 data = eval('(' + data +')');
				                      if (data.success == true)  
				                          return false;  
				                      else  
				                          return true;  
				                 }  
							}
						},
						password:{
							required:true,
							rangelength:[3,12]
						},
						comfromPassword:{
							required:true,
							rangelength:[3,12],
							equalTo:'#focusedPassword'
						},
						email:{
							required:true,
							email:true,
							remote:{
								url:basePath +"reg/checkEmail.mvc",  
				                 type:"post",  
				                 dataType:"json",  
				                 dataFilter: function(data, type) {  
				                	 data = eval('(' + data +')');
				                      if (data.success == true)  
				                          return false;  
				                      else  
				                          return true;  
				                 }  
							}
							
						},
						age:{
							isnum:true
						},
						mobile:{
							isMobile:true,
							remote:{
								url:basePath +"reg/checkMobile.mvc",  
				                 type:"post",  
				                 dataType:"json",  
				                 dataFilter: function(data, type) {  
				                	 data = eval('(' + data +')');
				                      if (data.success == true)  
				                          return false;  
				                      else  
				                          return true;  
				                 }  
							}
						},
						qq:{
							isnum:true
						},
						address:{
							maxlength:150
						}
					},
					messages:{
						userName:{
							remote:'亲，此用户名已存在，请换个响亮的吧？',
							required:'请输入用户名',
							rangelength:jQuery.validator.format("用户名长度必须介于 {0} 和 {1} 之间的字符串")
						},
						nickName:{
							required:'请输入昵称',
							rangelength:jQuery.validator.format("昵称必须介于 {0} 和 {1} 之间的字符串"),
							isChinese:'只能是中文昵称',
							remote:'此昵称已经存在！'
						},
						password:{
							required:'请输入密码',
							rangelength:jQuery.validator.format("密码必须介于 {0} 和 {1} 之间的字符串")
						},
						comfromPassword:{
							required:'请输入确认密码',
							rangelength:jQuery.validator.format("确认密码必须介于 {0} 和 {1} 之间的字符串"),
							equalTo:'两次密码不一致'
						},
						email:{
							required:'邮箱不能为空',
							email:'邮箱格式不正确',
							remote:'此邮箱已经存在'
						},
						age:{
							isnum:'年龄只能是数字',
						},
						mobile:{
							isMobile:'手机号码格式不正确',
							remote:'此手机号码已被占用'
						},
						qq:{
							isnum:'QQ号码只能是数字'
						},
						address:{
							maxlength:jQuery.validator.format("地址不能大于{0}个字符")
						}
					},
					submitHandler:function() {
						
						Reg.ctrl.submitForm();
						return false;
						
					}
				});
				
			},
			submitForm:function(){
				
				var formData = $('#regForm').serialize();
				 $.ajax({
					   url:basePath + 'reg/doReg.mvc',
					   cache:false,
					   data:formData,
					   dataType:'json',
					   type:'POST',
					   error:function() {
						   alert('提交失败，请重试!');
					   },
					   success:function(data, textStatus, jqXHR) {
						   
						   if(data.success == true) {
							   $('#_messageModal').on('show.bs.modal', function (event) {
								   var modal = $(this)
								   modal.find('.modal-body span').text(data.message);
								 })
							   $('#_messageModal').modal('show');
							   $('#_messageModal').on('hidden.bs.modal', function (e) {
								 window.location.href = basePath + '/jsps/login.jsp';
							   });
							  
							   
						   } else if(data.success = false) {
							   $('#_messageModal').on('show.bs.modal', function (event) {
								   var modal = $(this)
								   modal.find('.modal-body span').text(data.message);
								 })
							   $('#_messageModal').modal('show');
						   } else {
						   alert('保存失败，请重试!');
						   }
					   }
				   });
				
			}
		}
};

$(function(){
	
	Reg.init();
});

